#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),_timer(NULL),counter(0)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{                                                                      //这里的最大值1000会失败。
    _progressDialog = new QProgressDialog(tr("进度条条"),tr("取消进度"),0,500,this);
    _progressDialog->setWindowTitle(tr("进度条窗口"));
    _progressDialog->setWindowModality(Qt::ApplicationModal);
    _progressDialog->show();
    _timer = new QTimer(this);
    connect(_timer,&QTimer::timeout,this,&MainWindow::update_progressDialog);
    connect(_progressDialog,&QProgressDialog::canceled,this,&MainWindow::cancelBtn);
    _timer->start(1);
}

void MainWindow::update_progressDialog()
{
    counter++;
    qDebug() << "_counter: " << counter ;
    if(counter > 500)
    {
        _timer->stop();
        delete _progressDialog;
        _progressDialog = NULL;
        delete _timer;
        _timer = NULL;
        counter = 0;
        return;
    }
    _progressDialog->setValue(counter);
}

void MainWindow::cancelBtn()
{
    _timer->stop();
    delete _progressDialog;
    _progressDialog = NULL;
    delete _timer;
    _timer = NULL;
    counter = 0;
}


